/**
 * 随机id
 * 时间戳+随机数
 * @returns {string}
 */
const randomId = () => {
  return Date.now().toString(36) + Math.random().toString(36).substr(2);
};

/**
 * 防抖
 * @param fn
 * @param wait
 * @param $target
 * @returns {function(...[*]=)}
 */
const debounce = function (fn, wait, $target = window) {
  var _args = arguments;
  if ($target.__timer_debounce) {
    clearTimeout($target.__timer_debounce);
    delete $target.__timer_debounce;
  }
  $target.__timer_debounce = setTimeout(() => {
    fn.apply(this, _args);
    delete $target.__timer_debounce;
  }, wait);
};

/**
 * 节流
 * @param {方法} handler
 * @param {间隔时间毫秒数} wait
 */
const throttle = (handler, wait) => {
  var lastTime = 0;
  return function () {
    var nowTime = new Date().getTime();
    if (nowTime - lastTime > wait) {
      handler.apply(this, arguments);
      lastTime = nowTime;
    }
  };
};

export default {
  randomId,
  debounce,
  throttle,
};
